package geektime.springbean.xml;

import geektime.springbean.xml.model.User;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class SpringbeanXmlDemoApplication implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication.run(SpringbeanXmlDemoApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<String> favorites = new ArrayList<>();
        favorites.add("running");
        favorites.add("singing");
        User u1 = new User("Tom", 23, favorites);
        System.out.println(u1.toString());

        User u2 = new User();
        u2.setName("Jerry");
        u2.setAge(27);
        u2.setFavorites(favorites);
        System.out.println(u2.toString());

        ApplicationContext ac = new ClassPathXmlApplicationContext("config/ApplicationContext.xml");
        // 通过构造器注入
        System.out.println(ac.getBean("u1"));
        // 通过setter注入
        System.out.println(ac.getBean("u2"));
    }
}
